using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Matrix.ServiceDiscovery.Enums;

namespace Matrix.ServiceDiscovery.Services
{
    /// <summary>
    /// 健康检查应用服务接口
    /// </summary>
    public interface IHealthCheckAppService : ICrudAppService<
        HealthCheckRecordDto,
        Guid,
        PagedAndSortedResultRequestDto,
        CreateHealthCheckRecordDto,
        UpdateHealthCheckRecordDto>
    {
        /// <summary>
        /// 执行健康检查
        /// </summary>
        Task<HealthCheckResultDto> PerformHealthCheckAsync(PerformHealthCheckDto input);

        /// <summary>
        /// 批量执行健康检查
        /// </summary>
        Task<List<HealthCheckResultDto>> PerformBatchHealthCheckAsync(BatchHealthCheckDto input);

        /// <summary>
        /// 获取服务实例健康检查历史
        /// </summary>
        Task<PagedResultDto<HealthCheckRecordDto>> GetHealthCheckHistoryAsync(HealthCheckHistoryDto input);

        /// <summary>
        /// 获取健康检查统计信息
        /// </summary>
        Task<HealthCheckStatisticsDto> GetHealthCheckStatisticsAsync(DateTime? startTime = null, DateTime? endTime = null);

        /// <summary>
        /// 获取服务健康状态趋势
        /// </summary>
        Task<HealthStatusTrendDto> GetHealthStatusTrendAsync(string serviceName, TimeSpan period);

        /// <summary>
        /// 获取不健康的服务实例
        /// </summary>
        Task<List<ServiceInstanceDto>> GetUnhealthyInstancesAsync();

        /// <summary>
        /// 配置健康检查
        /// </summary>
        Task ConfigureHealthCheckAsync(ConfigureHealthCheckDto input);

        /// <summary>
        /// 启用/禁用健康检查
        /// </summary>
        Task ToggleHealthCheckAsync(Guid serviceInstanceId, bool enabled);

        /// <summary>
        /// 清理过期的健康检查记录
        /// </summary>
        Task<int> CleanupOldRecordsAsync(int retentionDays = 30);

        /// <summary>
        /// 获取健康检查配置
        /// </summary>
        Task<HealthCheckConfigDto> GetHealthCheckConfigAsync(Guid serviceInstanceId);
    }

    /// <summary>
    /// 执行健康检查DTO
    /// </summary>
    public class PerformHealthCheckDto
    {
        /// <summary>
        /// 服务实例ID
        /// </summary>
        public Guid ServiceInstanceId { get; set; }

        /// <summary>
        /// 检查类型
        /// </summary>
        public HealthCheckType CheckType { get; set; }

        /// <summary>
        /// 检查端点
        /// </summary>
        public string CheckEndpoint { get; set; } = string.Empty;

        /// <summary>
        /// 超时时间（毫秒）
        /// </summary>
        public int TimeoutMs { get; set; } = 5000;

        /// <summary>
        /// 检查器标识
        /// </summary>
        public string? CheckerId { get; set; }

        /// <summary>
        /// 检查参数
        /// </summary>
        public Dictionary<string, object>? Parameters { get; set; }
    }

    /// <summary>
    /// 批量健康检查DTO
    /// </summary>
    public class BatchHealthCheckDto
    {
        /// <summary>
        /// 服务实例ID列表
        /// </summary>
        public List<Guid> ServiceInstanceIds { get; set; } = new();

        /// <summary>
        /// 超时时间（毫秒）
        /// </summary>
        public int TimeoutMs { get; set; } = 5000;

        /// <summary>
        /// 并行度
        /// </summary>
        public int Concurrency { get; set; } = 5;
    }

    /// <summary>
    /// 健康检查历史DTO
    /// </summary>
    public class HealthCheckHistoryDto : PagedAndSortedResultRequestDto
    {
        /// <summary>
        /// 服务实例ID
        /// </summary>
        public Guid? ServiceInstanceId { get; set; }

        /// <summary>
        /// 服务名称
        /// </summary>
        public string? ServiceName { get; set; }

        /// <summary>
        /// 检查类型
        /// </summary>
        public HealthCheckType? CheckType { get; set; }

        /// <summary>
        /// 健康状态
        /// </summary>
        public HealthStatus? HealthStatus { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }
    }

    /// <summary>
    /// 配置健康检查DTO
    /// </summary>
    public class ConfigureHealthCheckDto
    {
        /// <summary>
        /// 服务实例ID
        /// </summary>
        public Guid ServiceInstanceId { get; set; }

        /// <summary>
        /// 检查类型
        /// </summary>
        public HealthCheckType CheckType { get; set; }

        /// <summary>
        /// 检查端点
        /// </summary>
        public string CheckEndpoint { get; set; } = string.Empty;

        /// <summary>
        /// 检查间隔（秒）
        /// </summary>
        public int IntervalSeconds { get; set; } = 30;

        /// <summary>
        /// 超时时间（毫秒）
        /// </summary>
        public int TimeoutMs { get; set; } = 5000;

        /// <summary>
        /// 健康阈值（连续成功次数）
        /// </summary>
        public int HealthyThreshold { get; set; } = 2;

        /// <summary>
        /// 不健康阈值（连续失败次数）
        /// </summary>
        public int UnhealthyThreshold { get; set; } = 3;

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool Enabled { get; set; } = true;

        /// <summary>
        /// 检查配置参数
        /// </summary>
        public Dictionary<string, object>? Parameters { get; set; }
    }

    /// <summary>
    /// 健康检查结果DTO
    /// </summary>
    public class HealthCheckResultDto
    {
        /// <summary>
        /// 服务实例ID
        /// </summary>
        public Guid ServiceInstanceId { get; set; }

        /// <summary>
        /// 健康状态
        /// </summary>
        public HealthStatus HealthStatus { get; set; }

        /// <summary>
        /// 响应时间（毫秒）
        /// </summary>
        public long ResponseTimeMs { get; set; }

        /// <summary>
        /// HTTP状态码
        /// </summary>
        public int? HttpStatusCode { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 检查详情
        /// </summary>
        public Dictionary<string, object>? Details { get; set; }

        /// <summary>
        /// 检查时间
        /// </summary>
        public DateTime CheckTime { get; set; }

        /// <summary>
        /// 性能等级
        /// </summary>
        public PerformanceLevel PerformanceLevel { get; set; }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess => HealthStatus == HealthStatus.Healthy;
    }

    /// <summary>
    /// 健康检查统计信息DTO
    /// </summary>
    public class HealthCheckStatisticsDto
    {
        /// <summary>
        /// 总检查次数
        /// </summary>
        public long TotalChecks { get; set; }

        /// <summary>
        /// 成功检查次数
        /// </summary>
        public long SuccessfulChecks { get; set; }

        /// <summary>
        /// 失败检查次数
        /// </summary>
        public long FailedChecks { get; set; }

        /// <summary>
        /// 成功率（百分比）
        /// </summary>
        public double SuccessRate { get; set; }

        /// <summary>
        /// 平均响应时间（毫秒）
        /// </summary>
        public double AverageResponseTime { get; set; }

        /// <summary>
        /// 最大响应时间（毫秒）
        /// </summary>
        public long MaxResponseTime { get; set; }

        /// <summary>
        /// 最小响应时间（毫秒）
        /// </summary>
        public long MinResponseTime { get; set; }

        /// <summary>
        /// 健康状态分布
        /// </summary>
        public Dictionary<HealthStatus, long> StatusDistribution { get; set; } = new();

        /// <summary>
        /// 检查类型分布
        /// </summary>
        public Dictionary<HealthCheckType, long> CheckTypeDistribution { get; set; } = new();

        /// <summary>
        /// 性能等级分布
        /// </summary>
        public Dictionary<PerformanceLevel, long> PerformanceDistribution { get; set; } = new();
    }

    /// <summary>
    /// 健康状态趋势DTO
    /// </summary>
    public class HealthStatusTrendDto
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;

        /// <summary>
        /// 时间段数据点
        /// </summary>
        public List<HealthStatusDataPoint> DataPoints { get; set; } = new();

        /// <summary>
        /// 平均可用性（百分比）
        /// </summary>
        public double AverageAvailability { get; set; }

        /// <summary>
        /// 最大响应时间（毫秒）
        /// </summary>
        public long MaxResponseTime { get; set; }

        /// <summary>
        /// 平均响应时间（毫秒）
        /// </summary>
        public double AverageResponseTime { get; set; }
    }

    /// <summary>
    /// 健康状态数据点
    /// </summary>
    public class HealthStatusDataPoint
    {
        /// <summary>
        /// 时间点
        /// </summary>
        public DateTime Timestamp { get; set; }

        /// <summary>
        /// 健康状态
        /// </summary>
        public HealthStatus HealthStatus { get; set; }

        /// <summary>
        /// 响应时间（毫秒）
        /// </summary>
        public long ResponseTimeMs { get; set; }

        /// <summary>
        /// 检查次数
        /// </summary>
        public int CheckCount { get; set; }

        /// <summary>
        /// 成功次数
        /// </summary>
        public int SuccessCount { get; set; }
    }

    /// <summary>
    /// 健康检查配置DTO
    /// </summary>
    public class HealthCheckConfigDto
    {
        /// <summary>
        /// 服务实例ID
        /// </summary>
        public Guid ServiceInstanceId { get; set; }

        /// <summary>
        /// 检查类型
        /// </summary>
        public HealthCheckType CheckType { get; set; }

        /// <summary>
        /// 检查端点
        /// </summary>
        public string CheckEndpoint { get; set; } = string.Empty;

        /// <summary>
        /// 检查间隔（秒）
        /// </summary>
        public int IntervalSeconds { get; set; }

        /// <summary>
        /// 超时时间（毫秒）
        /// </summary>
        public int TimeoutMs { get; set; }

        /// <summary>
        /// 健康阈值
        /// </summary>
        public int HealthyThreshold { get; set; }

        /// <summary>
        /// 不健康阈值
        /// </summary>
        public int UnhealthyThreshold { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool Enabled { get; set; }

        /// <summary>
        /// 检查参数
        /// </summary>
        public Dictionary<string, object> Parameters { get; set; } = new();

        /// <summary>
        /// 最后检查时间
        /// </summary>
        public DateTime? LastCheckTime { get; set; }

        /// <summary>
        /// 最后健康时间
        /// </summary>
        public DateTime? LastHealthyTime { get; set; }
    }
}